package com.xxc.hs.util;

import android.app.Application;

import com.xxc.hs.bean.SourceRecord;
import com.xxc.hs.db.SourceDao;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

@SuppressWarnings("ALL")
public class SourceTraceViewModel extends AndroidViewModel {
    private SourceDao sourceDao;
    private LiveData<List<SourceRecord>> sourceRecords;
    private String productBatch;

    public SourceTraceViewModel(@NonNull Application application, String batch) {
        super(application);
        sourceDao = SourceDao.getInstance(application);
        productBatch = batch;
        sourceRecords = sourceDao.getRecordsByBatch(batch);
    }

    public LiveData<List<SourceRecord>> getSourceRecords() {
        return sourceRecords;
    }

    public LiveData<List<SourceRecord>> getRecordsByDate(String date) {
        return sourceDao.getRecordsByDate(date);
    }

    public void insertRecord(SourceRecord record) {
        sourceDao.insertRecord(record, rowId -> {
            // 插入完成后刷新数据
            sourceRecords = sourceDao.getRecordsByBatch(record.getProductBatch());
        });
    }
}